import Request, {
	baseUrl
} from '../../util/pocky-request/index'
export default {
	namespaced: true,
	state: {
		login: uni.getStorageSync('login') || false,
		token: uni.getStorageSync('token') || '',
		user: uni.getStorageSync('user') || '',
		teacherId: uni.getStorageSync('teacherId') || ''
	},
	actions: {
		async loginAction(context, userAccount) {
			let e = { authProvider: 'LivePushEndPointProvider', providerAccessCode: JSON.stringify(userAccount) }
			let result = await Request().request({
				url: 'api/TokenAuth/LivePushEndPointAuthenticate',
				method: 'POST',
				allow: true,
				data: e
			});
			if (result) {
				if (!result.waitingForActivation && result.accessToken) {
					context.commit('login', {
						token: result.accessToken,
						user: result.teacherName,
						teacherId:result.teacherId
					})
					return {needToChangepassowrd: result.needToChangepassowrd, success: true};
				} else {
					uni.showToast({
						icon: 'none',
						title: '您输入的账号或密码不正确'
					})
					return {needToChangepassowrd: result.needToChangepassowrd, success: false};
				}
			}
		}
	},
	mutations: {
		login(state, userInfo) {
			uni.setStorageSync('login', true);
			state.login = true;
			if (userInfo.token) {
				uni.setStorageSync('token', userInfo.token);
				state.token = userInfo.token;
				if(userInfo.user){
					uni.setStorageSync('user', userInfo.user);
					state.user = userInfo.user
				}
				uni.setStorageSync('teacherId',userInfo.teacherId);
				state.teacherId=userInfo.teacherId;
			}
		},
		logout(state) {
			console.log('登出')
			uni.setStorageSync('login', false)
			state.login = false
			uni.removeStorageSync('token')
			state.token = ''
			uni.removeStorageSync('user')
			state.user = ''
			uni.removeStorageSync('teacherId')
			state.teacherId = ''
		}
	}
}
